﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class PreAllocateRuleDetailBLL : IBaseInfoDetail
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IPreallocateRuleDetailDA preallocateRuleDetailDA = DataAccess.CreatePreallocateRuleDetailDA();
        private PubParasInfo pubParasInfo;

        public PreAllocateRuleDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public PreAllocateRuleDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public PreAllocateRuleDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> mainID, List<string> subId, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if (mainID.Count != subId.Count)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2430_003", this.pubParasInfo.UserLanguage);
                }
                List<PreallocateRuleDetailInfo> preallocateRuleDetailInfos = new List<PreallocateRuleDetailInfo>();
                PreallocateRuleDetailInfo preallocateRuleDetailInfo = null;
                for (i = 0; i < mainID.Count; i++)
                {
                    preallocateRuleDetailInfo = preallocateRuleDetailDA.GetPreallocateRuleDetailByID(dataBase, tran, mainID[i], subId[i], this.dataBaseID[0], this.pubParasInfo);
                    preallocateRuleDetailInfos.Add(preallocateRuleDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                preallocateRuleDetailDA.DeletePreallocateRuleDetail(dataBase, tran, mainID, subId, this.dataBaseID);
                for (i = 0; i < mainID.Count; i++)
                {
                    auditDataBLL.InsertAuditData<PreallocateRuleDetailInfo>(dataBase, tran, preallocateRuleDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "PREALLOCATE_RULE_DETAIL", this.dataBaseID[0]);
                    string CS00002 = temp;
                    temp = CS00002 + mainID[i] + " , " + subId[i] + " ;";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return preallocateRuleDetailDA.GetAllPreallocateRuleDetail(this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            PreallocateRuleDetailQueryEntity preallocateRuleDetailQuery = obj as PreallocateRuleDetailQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(preallocateRuleDetailQuery.PreallocateRuleID))
            {
                sqlWhere.Add(" PREALLOCATE_RULE_ID " + preallocateRuleDetailQuery.PreallocateRuleID);
            }
            if (!string.IsNullOrEmpty(preallocateRuleDetailQuery.LineID))
            {
                sqlWhere.Add(" LINE_ID " + preallocateRuleDetailQuery.LineID);
            }
            if (!string.IsNullOrEmpty(preallocateRuleDetailQuery.Descr))
            {
                sqlWhere.Add(" DESCR " + preallocateRuleDetailQuery.Descr);
            }
            if (!string.IsNullOrEmpty(preallocateRuleDetailQuery.PreallocateStrategy))
            {
                sqlWhere.Add(" PREALLOCATE_STRATEGY " + preallocateRuleDetailQuery.PreallocateStrategy);
            }
            if (!string.IsNullOrEmpty(preallocateRuleDetailQuery.UomID))
            {
                sqlWhere.Add(" UOM_ID " + preallocateRuleDetailQuery.UomID);
            }
            if (!string.IsNullOrEmpty(preallocateRuleDetailQuery.PreallocatePickCode))
            {
                sqlWhere.Add(" PREALLOCATE_PICK_CODE " + preallocateRuleDetailQuery.PreallocatePickCode);
            }
            if (!string.IsNullOrEmpty(preallocateRuleDetailQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + preallocateRuleDetailQuery.IsActive);
            }
            return preallocateRuleDetailDA.GetPreallocateRuleDetailByQueryList(sqlWhere, preallocateRuleDetailQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetDetailInfoByID(string mainID, string subID, string pk3)
        {
            return preallocateRuleDetailDA.GetPreallocateRuleDetailByID(mainID, subID, this.dataBaseID[0], this.pubParasInfo);
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            List<PreallocateRuleDetailInfo> preallocateRuleDetailInfos = preallocateRuleDetailDA.GetPreallocateRuleDetailListByID(mainID, this.dataBaseID[0], this.pubParasInfo);
            List<object> objs = null;
            if ((preallocateRuleDetailInfos != null) && (preallocateRuleDetailInfos.Count > 0))
            {
                objs = new List<object>();
                for (int i = 0; i < preallocateRuleDetailInfos.Count; i++)
                {
                    objs.Add(preallocateRuleDetailInfos[i]);
                }
            }
            return objs;
        }

        public string InsertDetail(object obj)
        {
            PreallocateRuleDetailInfo preallocateRuleDetail = obj as PreallocateRuleDetailInfo;
            string PreallocateRuleID = string.Empty;
            try
            {
                if (preallocateRuleDetailDA.CheckPreallocateRuleDetailIDUnique(preallocateRuleDetail.PreallocateRuleID, preallocateRuleDetail.LineID.ToString(), this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2430_002", this.pubParasInfo.UserLanguage, new List<string> { preallocateRuleDetail.PreallocateRuleID, preallocateRuleDetail.LineID.ToString() });
                }
                if (preallocateRuleDetail == null)
                {
                    return PreallocateRuleID;
                }
                preallocateRuleDetail.UpdatedBy = preallocateRuleDetail.CreatedBy;
                preallocateRuleDetail.CreatedDate = DateTime.Now;
                preallocateRuleDetail.UpdatedDate = DateTime.Now;
                if (preallocateRuleDetailDA.InsertPreallocateRuleDetail(preallocateRuleDetail, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<PreallocateRuleDetailInfo>(null, preallocateRuleDetail, AuditAction.Insert, this.pubParasInfo.UserID, "PREALLOCATE_RULE_DETAIL", this.dataBaseID[0]);
                    PreallocateRuleID = preallocateRuleDetail.PreallocateRuleID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return PreallocateRuleID;
        }

        public string UpdateDetail(object obj)
        {
            string PreallocateRuleID = string.Empty;
            try
            {
                PreallocateRuleDetailInfo preallocateRuleDetail = obj as PreallocateRuleDetailInfo;
                preallocateRuleDetail.UpdatedBy = preallocateRuleDetail.CreatedBy;
                preallocateRuleDetail.CreatedDate = DateTime.Now;
                preallocateRuleDetail.UpdatedDate = DateTime.Now;
                PreallocateRuleDetailInfo oldPreallocateRuleDetailInfo = preallocateRuleDetailDA.GetPreallocateRuleDetailByID(preallocateRuleDetail.PreallocateRuleID, preallocateRuleDetail.LineID.ToString(), this.dataBaseID[0], this.pubParasInfo);
                if (preallocateRuleDetailDA.UpdatePreallocateRuleDetail(preallocateRuleDetail, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<PreallocateRuleDetailInfo>(oldPreallocateRuleDetailInfo, preallocateRuleDetail, AuditAction.Update, this.pubParasInfo.UserID, "PREALLOCATE_RULE_DETAIL", this.dataBaseID[0]);
                    PreallocateRuleID = preallocateRuleDetail.PreallocateRuleID + " , " + preallocateRuleDetail.LineID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return PreallocateRuleID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

